<template>
  <div class="goods-item">
    <div style="">
      <div class="goods-img">
        <a @click="openGoodsDetail">
          <img :key="goods.image" v-lazy="attachUrl(goods.image)" :alt="goods.spuName">
        </a>
      </div>
      <h6 class="goods-title">{{ goods.model }}</h6>
      <h6 class="goods-title">{{ goods.spuName }}</h6>
      <!--<h3 class="sub-title ellipsis">{{ goods.title }}</h3>-->
      <div class="goods-price pr">
        <div class="ds pa">
          <a @click="handleAddGoods">
            <mall-button text="加入购物车" style="margin: 0 5px" />
          </a>
          <!--<mall-button text="加入购物车" style="margin: 0 5px" class-style="main-btn" @btnClick="openGoodsDetail" />-->
        </div>
        <el-row>
          <el-col :span="12" style="padding-left:10px;text-align:center">
            <p><span class="label">零售价:</span><span>{{ Number(goods.price).toFixed(2) }}</span></p>
            <p><span class="label">批发价:</span><span>{{ Number(goods.wholesalePrice).toFixed(2) }}</span></p>
            <p><span class="label">库存:</span><span>{{ Number(goods.stock).toFixed(2) }}</span></p>
          </el-col>
          <el-col :span="12" style="padding-left:10px;text-align:center">
            <p><mall-button text="普通物流" style="width: 100px;" class-style="main-btn" /></p>
            <p><mall-button text="常规备货" style="width: 100px;" class-style="main-btn" /></p>
          </el-col>
        </el-row>
      </div>
    </div>
  </div>
</template>
<script>
import { mapGetters } from 'vuex'
import MallButton from '@/components/MallButton'
import CartApi from '@/api/order/CartApi.js'
import GoodsApi from '@/api/goods/GoodsApi.js'

export default {
  components: {
    MallButton
  },
  props: {
    goods: {
      type: Object,
      default: function() {
        return { price: 0 }
      }
    }
  },
  data() {
    return {}
  },
  computed: {
    ...mapGetters([
      'token',
      'userId',
      'showCart',
      'showMoveImg'
    ])
  },
  mounted() {
  },
  methods: {
    openGoodsDetail() {
      const spuId = this.goods.spuId || this.goods.id
      window.open('//' + window.location.host + `/#/goodsDetail?spuId=${spuId}`)
    },
    // 添加购物车
    handleAddGoods() {
      const spuId = this.goods.spuId || this.goods.id
      GoodsApi.getSpu(spuId).then(res => {
        if (res.data) {
          const sku = res.data.skuList[0]
          if (!this.showMoveImg && this.token) { // 动画是否在运动
            CartApi.addGoods({ skuId: sku.id }).then(res => {
              this.$store.dispatch('cart/InitCart')
            })
            if (!this.showCart) {
              this.$store.dispatch('cart/ShowCart', true)
            }
          } else {
            this.$router.push({ path: '/cart' })
          }
        }
      })
    }
  }
}
</script>

<style lang="scss" rel="stylesheet/scss" scoped>
@import "src/styles/mixin";
@import "src/styles/theme";

.goods-item {
  background: #fff;
  width: 25%;
  transition: all .5s;
  height: 430px;
  &:hover {
    transform: translateY(-3px);
    box-shadow: 1px 1px 20px #999;
    .goods-price p {
      display: none;
    }
    .ds {
      display: flex;
    }
  }
  .ds {
    @extend %block-center;
    width: 100%;
    display: none;
  }

  .goods-img {
    img {
      margin: 50px auto 10px;
      @include wh(206px);
      display: block;
    }
  }
  .goods-price {
    margin: 15px 0;
    height: 30px;
    text-align: center;
    line-height: 30px;
    color: #d44d44;
    font-family: Arial;
    font-size: 18px;
    font-weight: 700;
  }
  .goods-title {
    line-height: 16px;
    font-size: 14px;
    color: #424242;
    margin: 0 auto;
    padding: 2px 14px;
    text-align: center;
    overflow: hidden;
  }
  h3 {
    text-align: center;
    line-height: 12px;
    font-size: 12px;
    color: #d0d0d0;
    padding: 2px;
  }
  span {
    line-height: 13px;
    font-size: 13px;
  }
  .label {
    display:inline-block;
    width:60px;
    padding-right: 10px;
  }
}
</style>
